package co.yixiang.controller.buyer.order;

import cn.hutool.core.collection.CollectionUtil;
import co.yixiang.common.enums.ResultCode;
import co.yixiang.common.exception.ServiceException;
import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.vo.ResultMessage;
import co.yixiang.modules.order.order.entity.domain.YxStoreOrder;
import co.yixiang.modules.order.order.service.YxStoreOrderService;
import co.yixiang.modules.payment.entity.enums.PaymentClientEnum;
import co.yixiang.modules.payment.entity.enums.PaymentMethodEnum;
import co.yixiang.modules.payment.kit.CashierSupport;
import co.yixiang.modules.payment.kit.dto.PayParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 买家端,收银台接口
 *
 * @author Chopper
 * @since 2020-12-18 16:59
 */
@Slf4j
@RestController
@Api(tags = "买家端,支付相关接口")
@RequestMapping("/payment/cashier")
public class CashierController {

    @Autowired
    private CashierSupport cashierSupport;
    @Autowired
    private YxStoreOrderService orderService;

//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "client", value = "客户端类型", paramType = "path", allowableValues = "PC,H5,WECHAT_MP,APP")
//    })
//    @GetMapping(value = "/tradeDetail")
//    @ApiOperation(value = "获取支付详情")
//    public ResultMessage<CashierParam> paymentParams(@Validated PayParam payParam) {
//        CashierParam cashierParam = cashierSupport.cashierParam(payParam);
//        return ResultUtil.data(cashierParam);
//    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "交易编号", paramType = "query")
    })
    @GetMapping(value = "/pay")
    @ApiOperation(value = "支付")
    public ResultMessage payment(HttpServletRequest request, HttpServletResponse response, @Validated PayParam payParam) {
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf("WECHAT");
        PaymentClientEnum paymentClientEnum = PaymentClientEnum.valueOf("MP");

        final List<YxStoreOrder> byTradeSn = orderService.getByTradeSn(payParam.getSn());
        if (CollectionUtil.isEmpty(byTradeSn) || byTradeSn.size() != 1) {
            throw new ServiceException(ResultCode.PAY_NOT_EXIST_ORDER);
        }
        payParam.setSn(byTradeSn.get(0).getSn());
        try {
            return cashierSupport.payment(paymentMethodEnum, paymentClientEnum, request, response, payParam);
        } catch (ServiceException se) {
            log.info("支付异常", se);
            throw se;
        } catch (Exception e) {
            log.error("收银台支付错误", e);
        }
        return null;


    }

//    @ApiOperation(value = "支付回调")
//    @RequestMapping(value = "/callback/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
//    public ResultMessage<Object> callback(HttpServletRequest request, @PathVariable String paymentMethod) {
//
//        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);
//        cashierSupport.callback(paymentMethodEnum, request);
//        return ResultUtil.success(ResultCode.PAY_SUCCESS);
//    }

    @ApiOperation(value = "支付异步通知")
    @RequestMapping(value = "/notify/WECHAT", method = {RequestMethod.GET, RequestMethod.POST})
    public void notify(HttpServletRequest request) {
        System.out.println("支付回调");
        cashierSupport.notify(PaymentMethodEnum.WECHAT, request);
    }

    @ApiOperation(value = "查询支付结果")
    @GetMapping(value = "/result")
    public ResultMessage<Boolean> paymentResult(PayParam payParam) {
        return ResultUtil.data(cashierSupport.paymentResult(payParam));
    }
}

